<template>

	<div class="user">
		<!--搜索表单-->
		<div class="common-seach-wrap">
			<el-form size="small" :inline="true" :model="formInline" class="demo-form-inline">
				<el-form-item label="">
					<el-select v-model="formInline.status" placeholder="活动状态">
						<el-option label="全部" value="-1"></el-option>
						<el-option v-for="(item, index) in status" :key="index" :label="item"
							:value="index"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label=""><el-input v-model="formInline.title"
						placeholder="请输入活动标题"></el-input></el-form-item>
				<el-form-item><el-button icon="Search" @click="onSubmit">查询</el-button></el-form-item>
			</el-form>
		</div>

		<!--添加商品-->
		<div class="common-level-rail">
			<el-button size="small" icon="Plus" type="primary" @click="addClick">添加活动</el-button>
		</div>

		<div class="product-content">
			<el-form ref="form" :model="form" label-width="100px">
				<div class="table-wrap">
					<el-table size="small" :data="tableData" border style="width: 100%" v-loading="loading">
						<el-table-column prop="title" label="活动名称"></el-table-column>
						<el-table-column prop="start_time_text" label="开始日期" width="150"></el-table-column>
						<el-table-column prop="end_time_text" label="结束时间" width="150"></el-table-column>
						<el-table-column prop="join_end_time_text" label="报名截止日期" width="150"></el-table-column>
						<el-table-column prop="status_text" label="活动状态" width="120"></el-table-column>
						<el-table-column prop="product_num" label="商品数" width="70"></el-table-column>
						<el-table-column prop="product_audit_num" label="待审核" width="70">
							<template #default="scope">
								<span v-if="scope.row.product_audit_num == 0">0</span>
								<span v-if="scope.row.product_audit_num > 0" class="fbb"
									@click="toAudit(scope.row.seckill_activity_id)">{{ scope.row.product_audit_num }}</span>
							</template>
						</el-table-column>
						<el-table-column prop="total_sales" label="订单数" width="70"></el-table-column>
						<el-table-column prop="create_time" label="创建时间" width="150"></el-table-column>
						<el-table-column fixed="right" label="操作" width="90">
							<template #default="scope">
								<el-button @click="editClick(scope.row)" link type="primary" size="small">编辑</el-button>
								<el-button @click="deleteClick(scope.row)" link type="primary"
									size="small">删除</el-button>
							</template>
						</el-table-column>
					</el-table>
				</div>
			</el-form>
			<!--分页-->
			<div class="pagination">
				<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background
					:current-page="curPage" :page-size="pageSize" layout="total, prev, pager, next, jumper"
					:total="totalDataNumber"></el-pagination>
			</div>
		</div>
	</div>
</template>

<script>
	import {
		reactive,
		ref,
		onMounted,
		inject
	} from "vue";
	import {
		activeList,
		delActive
	} from "@/api/seckill.js";
	import {
		useRouter
	} from "vue-router";
	export default {
		components: {},
		inject: ["reload"],
		setup() {
			const baseConfirm = inject('$baseConfirm');
			const router = useRouter();
			/*是否加载完成*/
			const loading = ref(true);
			/*列表数据*/
			const tableData = ref([]);
			/*一页多少条*/
			const pageSize = ref(20);
			/*一共多少条数据*/
			const totalDataNumber = ref(0);
			/*当前是第几页*/
			const curPage = ref(1);
			/*搜索表单对象*/
			const form = reactive({});
			/*是否打开添加弹窗*/
			const open_add = ref(false);
			/*是否打开编辑弹窗*/
			const open_edit = ref(false);
			/*当前编辑的对象*/
			const userModel = ref({});
			const formInline = reactive({});
			const status = ["未开始", "进行中", "已结束"];
			/*已有的id*/
			const activeName = ref("has_audit");

			// 处理标签页点击事件
			const handleClick = (tab, event) => {
				curPage.value = 1;
				getTableList();
			};

			// 处理当前页码改变事件
			const handleCurrentChange = (val) => {
				curPage.value = val;
				getTableList();
			};

			// 处理每页显示条数改变事件
			const handleSizeChange = (val) => {
				curPage.value = 1;
				pageSize.value = val;
				getTableList();
			};

			// 获取数据列表
			const getTableList = () => {
				const Params = {
					...formInline
				};
				Params.page = curPage.value;
				Params.list_rows = pageSize.value;
				loading.value = true;
				activeList(Params, true)
					.then((data) => {
						loading.value = false;
						tableData.value = data.data.list.data;
						totalDataNumber.value = data.data.list.total;
					})
					.catch((error) => {});
			};

			// 处理表单提交
			const onSubmit = () => {
				curPage.value = 1;
				getTableList();
			};

			// 处理""按钮点击事件
			const addClick = () => {
				// 假设你在Vue 3中使用了Vue Router
				// 使用router.push而不是$router.push
				router.push("/plus/seckill/active/add");
			};

			// 处理"编辑活动"按钮点击事件
			const editClick = (row) => {
				const seckill_activity_id = row.seckill_activity_id;
				// 假设你在Vue 3中使用了Vue Router
				// 使用router.push而不是$router.push
				router.push({
					path: "/plus/seckill/active/edit",
					query: {
						seckill_activity_id: seckill_activity_id,
					},
				});
			};

			// 处理"删除"按钮点击事件
			const deleteClick = (row) => {
				// 使用Vue 3中新的组合式API的confirm方法
				// 你也可以使用el-message-box来实现确认对话框
				baseConfirm("此操作将永久删除该记录, 是否继续?", "提示", async () => {
					await delActive({
						seckill_activity_id: row.seckill_activity_id
					});
					ElMessage({
						message: "恭喜你，活动删除成功",
						type: "success",
					});
					onSubmit();
				})
			};

			// 处理"待审核"按钮点击事件
			const toAudit = (id) => {
				// 假设你在Vue 3中使用了Vue Router
				// 使用router.push而不是$router.push
				router.push({
					path: "/plus/seckill/index",
					query: {
						type: "audit",
						id: id,
					},
				});
				setTimeout(function() {
					router.go(0);
				}, 600);
			};
			onMounted(getTableList);

			return {
				loading,
				tableData,
				pageSize,
				totalDataNumber,
				curPage,
				form,
				open_add,
				open_edit,
				userModel,
				formInline,
				status,
				activeName,
				handleClick,
				handleCurrentChange,
				handleSizeChange,
				getTableList,
				onSubmit,
				addClick,
				editClick,
				deleteClick,
				toAudit,
				// created,
			};
		},
	};
</script>

<style>
	.fbb {
		border-bottom: 1px solid #409eff;
		color: #409eff;
		padding-bottom: 0px;
		width: 7px;
	}
</style>